package ch.zhaw.swp1.view;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import ch.zhaw.swp1.controller.MultiChannelController;
import ch.zhaw.swp1.model.message.Message;
import ch.zhaw.swp1.model.message.MessageType;
import ch.zhaw.swp1.model.status.Status;
import ch.zhaw.swp1.view.panels.MessageComposePanel;
import ch.zhaw.swp1.view.panels.MessageControlPanel;
import ch.zhaw.swp1.view.panels.MessageHeaderPanel;
/**
 * Main Window
 * @author Martin Trutmann
 */
public class GUI extends JFrame implements Observer {

	/**
	 * Default version
	 */
	private static final long serialVersionUID = 1L;

	private MessageHeaderPanel headerPanel;
	private MessageComposePanel composePanel;
	private MessageControlPanel controlPanel;

	/************************************************************************************/
	// Constructors
	/*----------------------------------------------------------------------------------*/
	/**
	 * Default constructor
	 */
	public GUI() {
		super("Message Center");
		init();
		addComponents();
		pack();
		setVisible(true);
		try {
			MultiChannelController.getInstance().loadMessages();
		} catch (Exception e) {
		}
	}

	/************************************************************************************/
	// Methods
	/*----------------------------------------------------------------------------------*/
	/**
	 * initializes the three main panels
	 */
	private void init() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());

		headerPanel = new MessageHeaderPanel();
		composePanel = new MessageComposePanel();
		controlPanel = new MessageControlPanel(this);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				try {
					MultiChannelController.getInstance().saveMessages();
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(composePanel, "Ihre Eingaben gehen verloren, wenn Sie das Fenster schliessen!");
				}
			}
		});

	}

	/**
	 * adds the three main panels to the frame
	 */
	public void addComponents() {
		add(headerPanel, BorderLayout.NORTH);
		add(composePanel, BorderLayout.CENTER);
		add(controlPanel, BorderLayout.SOUTH);
	}

	/**
	 * Send a message entered by the user
	 */
	public void sendMessage() {
		MultiChannelController.getInstance().processMessage(this);		
	}


	/**
	 * Reset Fields
	 */
	public void resetFields() {
		headerPanel.reset();
		composePanel.getCurrentPane().reset();
		controlPanel.reset();		
	}

	/************************************************************************************/
	// Setter/Getter
	/*----------------------------------------------------------------------------------*/
	public MessageType getMessageType() {		
		return composePanel.getCurrentPane().getMessageType();
	}
	public String getMessageText(){
		return composePanel.getCurrentPane().getMessageText();
	}
	public String getSender(){
		return headerPanel.getSender();
	}
	public String getRecipients(){
		return headerPanel.getRecipient();
	}
	public Date getSendDate(){
		return controlPanel.getSendDate();
	}
	public List<File> getAttachments(){
		return composePanel.getCurrentPane().getAttachments();
	}

	@Override
	public void update(Observable o, Object arg) {
		Message message = (Message) o;
		@SuppressWarnings("unchecked")
		List<Status> statusList = (List<Status>) arg;
		if(statusList.isEmpty()){
			resetFields();
			StatusOptionPane.showMessageSentDialog(message);
		}
		else {
			StatusOptionPane.showMessageStatusDialog(message);
		}
	}
}
